﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Text;

namespace Marmat.Foundation
{
    public class ColumnViewCollection : List<ColumnView>
    {

        public static implicit operator ColumnViewCollection(Type type)
        {
            var collection = new ColumnViewCollection();

            var members = type.GetMembers();
            foreach (var member in members)
            {
                ColumnView columnView = new ColumnView();
                if (member is FieldInfo field)
                {
                    columnView.Name = field.Name;
                }
                else if (member is PropertyInfo property)
                {
                    columnView.Name = property.Name;
                }
                else
                    continue;
#if NET45_OR_GREATER
                System.ComponentModel.DescriptionAttribute description = member.GetCustomAttribute<System.ComponentModel.DescriptionAttribute>();
                if (description != null)
                    columnView.Caption = description.Description;
#endif
                collection.Add(columnView);
            }
            return collection;
        }

        public static implicit operator ColumnViewCollection(DataTable data)
        {
            var collection = new ColumnViewCollection();
            foreach (DataColumn column in data.Columns)
            {
                ColumnView columnView = new ColumnView();
                columnView.Name = column.ColumnName;
                columnView.Caption = column.Caption;
                collection.Add(columnView);
            }
            return collection;
        }
    }
}
